using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CrystalDecisions.Web;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;

using SPL.Services;
using SPL.Entities;

public partial class rptDailyReport : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    try
    {
      if (UserRole == (int)RoleOptions.User) return;

      switch (Request["r"])
      {
          //GPP report
          case "0":
          {
            if (UserDepartment == (int)DepartmentOptions.TVT && UserRole != (int)RoleOptions.Admin) return;

            Gpp obj = (new GppService()).GetById(long.Parse(Request["Id"]));

            TList<Gpp> lstGpp = new TList<Gpp>();
            lstGpp.Add(obj);

            ReportDocument rdDailyGPP = new ReportDocument();
            string pathReport = Server.MapPath(@"DailyGpp.rpt");
            rdDailyGPP.Load(pathReport);
            rdDailyGPP.SetDataSource(lstGpp.ToDataSet(false));
            //lstGpp.ToDataSet(false).WriteXml(@"C:\Inetpub\wwwroot\spl\SPL\SPL.Website\GppDS.xml", XmlWriteMode.WriteSchema);
            crvDailyReport.ParameterFieldInfo.Clear();
            //crvDailyReport.DisplayGroupTree = false;
            //crvDailyReport.DisplayToolbar = false;
            //crvDailyReport.BackColor = System.Drawing.Color.DarkKhaki;
            //crvDailyReport.BorderColor = System.Drawing.Color.Blue;
            //crvDailyReport.ForeColor = System.Drawing.Color.White;
            crvDailyReport.ReportSource = rdDailyGPP;
            crvDailyReport.DataBind();
            crvDailyReport.RefreshReport();

            break;
          }

          //TVT
          case "1":
          {
            if (UserDepartment == (int)DepartmentOptions.GPP && UserRole != (int)RoleOptions.Admin) return;

            Tvt obj = (new TvtService()).GetById(long.Parse(Request["Id"]));

            TList<Tvt> lstTvt = new TList<Tvt>();
            lstTvt.Add(obj);

            ReportDocument rdDailyTVT = new ReportDocument();
            string pathReport = Server.MapPath(@"DailyTvt.rpt");
            rdDailyTVT.Load(pathReport);
            rdDailyTVT.SetDataSource(lstTvt.ToDataSet(false));
            //lstTvt.ToDataSet(false).WriteXml(@"C:\Inetpub\wwwroot\spl\SPL\SPL.Website\TvtDS.xml", XmlWriteMode.WriteSchema);
            crvDailyReport.ParameterFieldInfo.Clear();
            //crvDailyReport.DisplayGroupTree = false;
            //crvDailyReport.DisplayToolbar = false;
            crvDailyReport.ReportSource = rdDailyTVT;
            crvDailyReport.DataBind();
            crvDailyReport.RefreshReport();

            break;
          }
      }
    }
    catch (Exception ex)
    {
      Response.Write(ex.Message);
    }
  }
  protected void btnBack_Click(object sender, EventArgs e)
  {
    Response.Redirect("default.aspx");
  }

  #region Properties
  public int UserRole
  {
    get
    {
      try
      {
        string userData = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
        return int.Parse(userData.Split('|')[0]);
      }
      catch
      {
        return 0;
      }
    }
  }
  public int UserDepartment
  {
    get
    {
      try
      {
        string userData = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
        return int.Parse(userData.Split('|')[1]);
      }
      catch
      {
        return 0;
      }
    }
  }
  #endregion

  #region role, department enumerations, helpers
  public enum DepartmentOptions { GPP = 1, TVT = 2, KVT = 3 };
  public enum RoleOptions { Admin = 1, Manager = 2, Leader = 3, User = 4 };
  public enum StatusOptions { New = 1, Checked = 2, Approved = 3 };  
  #endregion
}
